package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author majun
 * @package sky-take-out
 * @date 2024/5/14 : 上午11:22
 */
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @return
     */
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    @PostMapping
    public Result save(@RequestBody SetmealDTO setmealDTO) {

        log.info("新增套餐：{}", setmealDTO);

        setmealService.save(setmealDTO);

        return Result.success();

    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {

        log.info("套餐分页查询：{}", setmealPageQueryDTO);

        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<SetmealVO> getFromById(@PathVariable String id) {

        log.info("根据id查询套餐：{}", id);

        SetmealVO setmealVO = setmealService.getFromById(id);

        return Result.success(setmealVO);
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     * @return
     */
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {

        log.info("批量删除套餐:{}", ids);

        setmealService.delete(ids);

        return null;

    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     * @return
     */
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    @PutMapping
    public Result update(@RequestBody SetmealDTO setmealDTO) {

        log.info("修改套餐:{}", setmealDTO);

        setmealService.update(setmealDTO);

        return Result.success();

    }

    /**
     * 套餐起售、停售
     *
     * @param status
     * @return
     */
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable String status, String id) {

        log.info("套餐起售、停售:{} {}", status, id);

        setmealService.startOrStop(status, id);

        return Result.success();

    }

}
